import _ from 'lodash';
import Util from './util.js';
import Config from './config.js';
const fwconfig = require('./config')

var captureImage = _.extend({}, _, {
    //拍照，successCallback拍照完成后的方法
    captureClick(successCallback) {
        let self = this;
        var bts = [
            {
                title: "拍摄"
            },
            {
                title: "从相册选择"
            }
        ];
        window.plus.nativeUI.actionSheet(
            {
                // title: "从相册选择",
                cancel: "取消",
                buttons: bts
            },
            function (e) {
                if (e.index == 1) {
                    // 拍摄
                    self.getImage(successCallback);
                } else if (e.index == 2) {
                    // 从相册选取
                    self.galleryImg(successCallback);
                }
            }
        );
    },
    getImage(successCallback) {
        let self = this;
        // console.log("开始拍照：");
        // var filename = "_doc/camera/" + self.$util.random() + ".jpg";
        var cmr = window.plus.camera.getCamera();
        //设置像素
        var res = cmr.supportedVideoResolutions[0];
        cmr.captureImage(
            function (p) {
                window.plus.io.resolveLocalFileSystemURL(
                    p,
                    function (entry) {
                        self.compressImage(entry.toLocalURL(), entry.name, successCallback);
                    },
                    function (e) {
                        console.error("读取拍照文件错误：" + e.message);
                    }
                );
            },
            function (e) {
                console.error("失败：" + e.message);
            },
            {
                // filename: filename,
                // index: 1
                filter: "image",
                resolution: res,
                format: ".jpg",
            }
        );
    },
    galleryImg(successCallback) {
        let self = this;
        window.plus.gallery.pick(
            function (e) {
                var name = e.substr(e.lastIndexOf("/") + 1);
                // 上传图片保存
                self.compressImage(e, name, successCallback);
            },
            function (e) {
                console.log("取消选择图片");
            },
            {
                filter: "image"
            }
        );
    },
    //压缩图片
    compressImage(url, filename, successCallback) {
        let self = this;
        var name = "_doc/image/" + filename;
        window.plus.zip.compressImage(
            {
                src: url, //src: (String 类型 )压缩转换原始图片的路径
                dst: name, //压缩转换目标图片的路径
                quality: 40, //quality: (Number 类型 )压缩图片的质量.取值范围为1-100
                overwrite: true //overwrite: (Boolean 类型 )覆盖生成新文件
            },
            function (zip) {
                //页面显示图片
                self.loadImg(name, successCallback);
            },
            function (error) {
                window.plus.nativeUI.toast("压缩图片失败，请稍候再试");
            }
        );
    },
    loadImg(url, successCallback) {
        window.plus.io.resolveLocalFileSystemURL(url, function (entry) {
            entry.file(function (file) {
                var fileReader = new plus.io.FileReader();
                fileReader.readAsDataURL(file);
                fileReader.onloadend = function (e) {
                    var filepath = "_doc/image/" + file.name;
                    successCallback(e, filepath);
                };
            });
        });
    },
    //上传文件,filepath压缩后的图片存储的路径，uploadUrl上传的服务器的路径，paramelist参数[{key:"id","1"},{key:"name","jack"}]
    createUpload(filepath, uploadUrl, paramelist, afterupload) {
        let self = this;
        var url = fwconfig.api.upload + uploadUrl;
        var task = window.plus.uploader.createUpload(
            url,
            {
                method: "POST",
                blocksize: 204800,
                priority: 100
            },
            function (t, status) {
                console.log(t);
                // 上传完成
                if (status == 200) {
                    var rlt = JSON.parse(t.responseText);
                    afterupload(rlt);
                } else {
                    self.$f7.dialog.toast("网络连接失败");
                }
            }
        );
        task.addFile(filepath, {
            key: ""
        });
        paramelist.forEach(function (value) {
            task.addData(value.key, value.value);
        });
        task.start();
    }
});

export default captureImage;
